package com.pn.controller;

import com.pn.entity.*;
import com.pn.service.*;

import com.pn.utils.PageBean;
import com.pn.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private UnitService unitService;
   //显示单位列表
   @GetMapping("/unit-list")
   public Result unitList(){
       List<Unit> result = unitService.unitList();
       return Result.ok(result);
   }

    //显示产地列表
    @Autowired
    private PlaceService placeService;
    @GetMapping("/place-list")
    public Result placeList(){
        List<Place> places = placeService.placeList();
        return Result.ok(places);
    }

    //显示供货商表
    @Autowired
    private SupplyService supplyService;
    @GetMapping("/supply-list")
    public Result supplyList(){
        List<Supply> supplyList = supplyService.supplyList();
        return Result.ok(supplyList);
    }

    //显示品牌表
    @Autowired
    private BrandService brandService;
    @GetMapping("/brand-list")
    public Result brandList(){
        List<Brand> brandList = brandService.brandList();
        return Result.ok(brandList);
    }

    @Autowired
    private StoreService storeService;
   //显示仓库表
   @GetMapping("/store-list")
   public Result storeList(){
       List<Store> storeList = storeService.storeList();
       return Result.ok(storeList);
   }

   //显示客户表
   @Autowired
   private ClientService clientService;
   @GetMapping("client-list")
   public Result clientList(){
      List<Client> clientList = clientService.clietList();
      return Result.ok(clientList);
   }

    //查询商品分类树
    @GetMapping("/category-tree")
    public Result categoryTree(){
        List<ProductType> productTypes = productService.categoryTree();
        return Result.ok(productTypes);
    }

    //商品列表展示
    @GetMapping("/product-page-list")
    public Result<PageBean<Product>> productPageList(Product product){
        PageBean pageBean = productService.productPageList(product);
        return Result.ok(pageBean);
    }

    //图片上传到idea文件中
    @Value("${file.upload-path}")
    private String uploadPath;

    @CrossOrigin //表示url/img-upload 接口允许跨域请求
    @RequestMapping("/img-upload")
    public Result uploadImage(MultipartFile file){

        try {
            //解析类路径并拿到封装类路径的File对象
            File uploadDirFile = ResourceUtils.getFile(uploadPath);
            //拿到图片上传到目录路径的本地磁盘路径
            String uploadDirPath = uploadDirFile.getAbsolutePath();
            //拿到上传的图片名称
            String filename = file.getOriginalFilename();
            //拿到上传文件要保存到磁盘文件的路径
            String uploadFilePath = uploadDirPath + "\\" + filename;
            //上传图片
            file.transferTo(new File(uploadFilePath));
            return Result.ok("图片上传成功!");
        } catch (IOException e) {
            return Result.err(Result.CODE_ERR_BUSINESS,"图片上传失败!");
        }
    }

    //添加商品
    @PostMapping("/product-add")
    public Result addProduct(@RequestBody Product product){
        Result result = productService.saveProduct(product);
        return result;
    }

    //商品上下架
    @PutMapping("/state-change")
    public Result changeProductState(@RequestBody Product product){
        return productService.changeProductStateBId(product.getProductId(),product.getUpDownState());
    }

    //删除商品
    @DeleteMapping("/product-delete/{productId}")
    public Result deleteProduct(@PathVariable Integer productId){
        productService.deleteProductByIds(Arrays.asList(productId));
        return Result.ok("商品删除成功");
    }

    //删除多个商品
    @DeleteMapping("product-list-delete")
    public Result ProductListDelete(@RequestBody List<Integer> ids){
        productService.deleteProductByIds(ids);
        return Result.ok("批量商品删除成功");
    }

    //修改商品
    @PutMapping("product-update")
    public Result ProductUpdate(@RequestBody Product product){
       return productService.productUpdate(product);
    }



}
